import axios from '@/utils/request'

const filterUserRemoteMethod = (query, context) => {
  axios
    .get(`/manage/users`, { params: {
      '@filter': `
        entity.getUsername() == "${query}"
        || entity.getId() == "${query}"
      `
    }})
    .then(res => {
      context.data = res.data.map(v => {
        return { value: v.id, label: `${v.id}: ${v.__toString}` }
      })
    })
}

const TYPE = {
  '-0-': '--- 普通 ---',
  'deposit': '门店入金',
  'burn': '提现成功',
  'burn-to-destroy': '申请提现',
  'collect': '代收',
  'salary': '工资发放',
  'upload': '导入发放',
  'transfer': '转账',
  'deposit-fee': '手续费',
  'rollback': '回滚',
  'administrator': '管理员调整',
  'bond': '保证金',
  'bond-return': '退还保证金',
  'reimbursement': '报销',
  'marketing-expenses': '营销费用',

  '-1-': '--- 订单分账 ---',
  'order-settle': '订单分账',
  'order-settle-agent': '酒代分成',
  'order-settle-place-manager': '管理员分成',
  'order-settle-platform': '平台分成',
  'order-settle-in-charge': '负责人分成',
  'order-settle-place': '场所分成',
  'order-settle-company': '资源公司分成',
  'order-settle-leader': '队长分成',
  'order-settle-broker': '经纪人分成',
  'order-settle-royalties': '提成',
  'order-settle-subsidy': '补贴',
  'order-settle-salary': '工资',
  'order-settle-booking': '订台分成',
  'order-settle-waiter': '服务员分成',
  'order-settle-store': '剩余余额',
  'order-settle-platform-fee': '平台手续费',

  '-2-': '--- 工资分成 ---',
  'salary-settle': '工资分成',
  'salary-settle-staff': '员工工资',
  'salary-settle-leader': '队长工资分成',
  'salary-settle-broker': '经纪人工资分成',

  '-3-': '--- 费用类型 ---',
  'onboard-fee': '入职费用',
  'faceid-authority-fee': '人脸核身费用',
  'insurance-fee': '保险费用',
  'blackhole-fee': '报销费用',
  'penalty-fee': '扣罚',
  'withdraw-fee': '提现手续费',
  'liquor-fee': '场所酒水费用'
}

export default {
  Transaction: {
    list: {
      export: {
        query: {
          '@display': `{
          'id' : 'entity.getId()',
          'hash' : 'entity.getHash()',
          'status' : '{ "failed": "失败", "pending": "等待", "success": "成功" }[entity.getStatus()]',
          'type' : '${JSON.stringify(TYPE)}[entity.getType()]',
          'amount' : 'entity.getAmount()',
          'from' : 'entity.getFrom().getUsername()',
          'to' : 'entity.getTo().getUsername()',
          'currency' : 'entity.getCurrency()',
          'comment' : 'entity.getComment()',
          'createdTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")'
        }`,
          limit: 10000000
        },
        label: {
          'id': 'id',
          'hash': '哈希',
          'status': '状态',
          'type': '类型',
          'amount': '金额',
          'from': '发送者',
          'to': '接收者',
          'currency': '货币',
          'comment': '备注',
          'createdTime': '创建时间'
        }
      },
      disabled_actions: ['new', 'lines'],
      query: {
        '@order': 'entity.id | DESC'
      },
      list_filter: {
        id: 'ID',
        hash: '哈希',
        type: Object.assign({ __label: '类型' }, TYPE),
        status: {
          '__label': '状态',
          'failed': '失败',
          'pending': '等待',
          'success': '成功'
        },
        from: {
          expression: `entity.getFrom().getId() == :value`,
          label: '发送者',
          type: 'remote-select',
          data: [],
          remoteMethod: filterUserRemoteMethod
        },

        to: {
          expression: `entity.getTo().getId() == :value`,
          label: '接收者',
          type: 'remote-select',
          data: [],
          remoteMethod: filterUserRemoteMethod
        },
        startTime: {
          expression: `
          entity.getCreatedTime() >= datetime.get(":value")
        `,
          label: '开始时间',
          type: 'datetime'
        },
        endTime: {
          expression: `
          entity.getCreatedTime() < datetime.get(":value")
        `,
          label: '结束时间',
          type: 'datetime'
        }
      },
      list_display: [
        'id',
        { property: 'hash', label: '哈希' },
        {
          property: 'status', label: '状态',
          component: {
            methods: {
              filter: (type, status) => [
                { value: 'failed', label: '失败', tagType: 'danger' },
                { value: 'pending', label: '等待', tagType: 'warning' },
                { value: 'success', label: '成功', tagType: 'success' }
              ].find(obj => obj.value === status)[type]
            },
            props: ['data'],
            render(h) {
              return (
                <el-tag type={this.filter('tagType', this.data)}>
                  {this.filter('label', this.data)}
                </el-tag>
              )
            }
          }
        },
        {
          property: 'type', label: '类型',
          component: {
            props: ['data'],
            render(h) {
              return <span>{TYPE[this.data]}</span>
            }
          }
        },
        {
          property: 'amount', label: '金额',
          component: {
            props: ['data'],
            render(h) {
              return <span>￥{this.data}</span>
            }
          }
        },
        { property: 'from', label: '发送者' },
        { property: 'to', label: '接收者' },
        { property: 'currency', label: '货币' },
        'comment',
        {
          property: 'attributes', label: '属性',
          component: {
            props: ['data'],
            data() {
              return {
                objects: Object.keys(this.data).map(
                  k => { return { key: k, value: this.data[k] } }
                )
              }
            },
            render(h) {
              return (
                <el-popover
                  width='300'
                  placement='right'
                  trigger='hover'
                >
                  <el-table data={this.objects}>
                    <el-table-column
                      label='键'
                      prop='key'
                    />
                    <el-table-column
                      label='值'
                      prop='value'
                    />
                  </el-table>
                  <el-button slot='reference' size='small'>查看</el-button>
                </el-popover>
              )
            }
          }
        },
        'createdTime'
      ]
    },
    form: {
      fields: [
        'id'
      ]
    }
  }
}
